<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="../style.css" type="text/css" />
</head>
<?php
		include_once "../inc/docroot.php";
		include_once PATH."inc/class.autores.inc.php";
		require PATH."inc/google-api-php-client-master/autoload.php";
		$autores = new autores($db);
		if(isset($_GET['submaster']))$submaster_id = $_GET['submaster'];
		if(isset($_GET['edit']))$edit = $_GET['edit'];
		if(isset($_GET['delete']))$delete = $_GET['delete'];
		
		//getch needed info
		$client_info = $autores->getClientInfo();
        $client_id = $client_info['client_id']; //'1097530924560-9dv7lmvrmlfsdrn3rbd8a197ci34n4og.apps.googleusercontent.com';
        $client_secret = $client_info['client_secret']; //'VMTuLVpXMyjbdi_EoyeJxUnr';
        $redirect_uri = $client_info['sub_master_redirect']; //'http://www.bytefact.com/mesbagapi.php';

        //init google client
        $client = new Google_Client();
        $client->setClientId($client_id);
        $client->setClientSecret($client_secret);
        $client->setRedirectUri($redirect_uri);

        $client->addScope("https://mail.google.com");
        $client->setApprovalPrompt('force');
        $client->setAccessType('offline');
        
        //init google service
        $service = new Google_Service_Gmail($client);
        
        if (isset($_REQUEST['logout'])) {
            unset($_SESSION['access_token']);
        }

        if (isset($_GET['code'])) { // we received the positive auth callback, get the token and store it in session
            $client->authenticate($_GET['code']);
            $_SESSION['access_token'] = $client->getAccessToken();
        }
        
        if (isset($_SESSION['access_token']) && $client->getAccessToken()) { // extract token from session and configure client
            $access_token = $client->getAccessToken();
            $profile = $service->users->getProfile('me');
            
            echo "<div align=\"center\">";
            echo "<br/>";
            $autores->editsubmaster($profile->emailAddress, $_SESSION['access_token']);
            echo "<br/>";
            $autores->editalias($profile->emailAddress, $_SESSION['access_token']);
            echo "</div>";
            
            if(isset($_SESSION['access_token']))unset($_SESSION['access_token']);
            if(isset($_GET['code']))unset($_GET['code']);
        }

?>

<body>
<div>
   <div class="autores_header">
<?php
       	if( !empty($_POST['name']) &&!empty($_POST['mailid']) && !empty($_POST['password']) && !empty($_POST['smtp']) && !empty($_POST['imap']) && isset($submaster_id) && isset($edit) && $edit == 1) {
        	echo "<br /><br />";
        	$complain = $autores->isPossibleAddSubmaster($_POST['mailid'],$submaster_id);
        	if($complain == "" ){
                echo $autores->editsubmaster($submaster_id);
                
                $gmail = "@gmail.com";
                if(strstr($_POST['mailid'],$gmail)!=false)
                {
                    $client->setLoginHint($_POST['mailid']);
                    $loginUrl = $client->createAuthUrl();
                    header('Location: ' . filter_var($loginUrl, FILTER_VALIDATE_URL));
                }
                else {
                ?>
                    <script>
                        window.opener.location.reload();
                    </script>
                <?php
                }
            }
            else echo $complain;
	   }
	   else if(isset($submaster_id) && isset($delete) && $delete == 1) {
        	echo "<br /><br />";
			echo $autores->deletesubmaster($submaster_id);
			?>
			<script>
			window.opener.location.reload();
			window.close();
            </script>
            <?php
	   }
	   
	   else if( !empty($_POST['name']) &&!empty($_POST['mailid']) && !empty($_POST['password']) && !empty($_POST['smtp']) && !empty($_POST['imap'])) {
        	echo "<br /><br />";
            $complain = $autores->isPossibleAddSubmaster($_POST['mailid']);
            if($complain == "" ){
                echo $autores->addsubmaster();
                
                $gmail = "@gmail.com";
                if(strstr($_POST['mailid'],$gmail)!=false)
                {
                    $client->setLoginHint($_POST['mailid']);
                    $loginUrl = $client->createAuthUrl();
                    header('Location: ' . filter_var($loginUrl, FILTER_VALIDATE_URL));
                }
                else {
                ?>
                    <script>
                        window.opener.location.reload();
                    </script>
                <?php
                }
            }
            else echo $complain;
	   }
	   else{
    	echo "<br />";
    	echo "<br />";
		if(!isset($delete))
   			echo "<label>Please fill all the fields.</label>";
	}
    echo "<br />";
   	echo "<br />";
   
	if(isset($submaster_id) && isset($edit) && $edit == 0){
		echo "<label> EDIT POPPING MAIL: " .$submaster_id."</label><br/>";
		echo "<label> NEW MAIL ID wll be updated to corresponding CAMPAIGN</label>";
	}
	else if(isset($submaster_id) && isset($delete) && $delete == 0){
		echo "<label> Are you sure to DELETE POPPING MAIL: " .$submaster_id."</label>";
		echo "<br/>";
		echo "<br/>";
		echo "<label> <font color=\"red\">It will deactivate the campaign configured with this Mail ID</font></label>";
		echo "<br/> <br/>";
	?>
    
   <form method="post" action="addnewsubmaster.php<?php if(isset($submaster_id) && isset($delete)) echo "?submaster=".$submaster_id."&delete=1";?>" name="deletesubmaster" id="deletesubmaster">
  <br /><br />
  <input type="submit" name="deletesubmaster" id="deletesubmaster" value="DELETE POPPING MAIL" class="btn btn--blue btn--m" />
	</div>
	<?php
    }
	else {
    	echo "<label> ADD A NEW POPPING MAIL </label>";
	}
	if(!isset($delete)){
?>
  </div>
    
  <form method="post" action="addnewsubmaster.php<?php if(isset($submaster_id) && isset($edit)) echo "?submaster=".$submaster_id."&edit=1";?>" name="addsubmasterform" id="addsubmasterform">
    <div style="font-size:9px;" align="center">
    <br />
      <br />
      <input type="text" name="name" id="name" style="text-align:center"/>
      <label for="name">Name</label>
      <br />
      <br />
      <input type="text" name="mailid" id="mailid" style="text-align:center"/>
      <label for="mailid">Email ID</label>
      <br />
      <br />
      <input type="text" name="password" id="password" style="text-align:center"/>
      <label for="password">Password</label>
      <br />
      <br />
      <select name="smtp" id="smtp" width="340" style="width: 340px; height:20px; font-size:13px;" height="20">
<?php 
		$autores->smtpselect();
?>
      </select></br>
      <label for="smtp">SMTP</label>
      <br />
      <br />
      <select name="imap" id="imap" width="340" style="width: 340px; height:20px; font-size:13px;" height="20">
<?php 
		$autores->imapselect();
?>
      </select></br>
      <label for="imap">IMAP</label>
      <br />
      <br />
      <br />
      <br/ >
      <input type="submit" name="addsubmasterinput" id="addsubmasterinput" value="Save Popping Mail" class="btn btn--blue btn--m" />
    </div>
  </form>
    <?php  } ?>
</div>
</body>


<SCRIPT LANGUAGE='JAVASCRIPT' TYPE='TEXT/JAVASCRIPT'>

</SCRIPT>
